/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

/**
 *
 * @author LUIS
 */
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import model.*;
import model.Character;
import model.Object;

public class ControlManager {	
	private static Map map;	
	private static ArrayList<String> characters = new ArrayList<String>();	
	private static ArrayList<String> inanimate = new ArrayList<String>();
	
	
	
	public static ArrayList<String> getCharacters() {
		return characters;
	}



	public static void setCharacters(ArrayList<String> characters) {
		ControlManager.characters = characters;
	}



	public static ArrayList<String> getInanimate() {
		return inanimate;
	}



	public static void setInanimate(ArrayList<String> inanimate) {
		ControlManager.inanimate = inanimate;
	}

	public ControlManager(){
		characters.add("Sniper");
		characters.add("Knight");
		characters.add("Sailor");
		characters.add("Hero");
		characters.add("Villager");
		
		inanimate.add("Inn");
		inanimate.add("Building");
		inanimate.add("Tree");
		inanimate.add("Mountain");
		inanimate.add("Rock");
                //this.loadMap("DB.xml");
	}
	public ControlManager(int a, int b){
		
		
		map=new Map(a,b);
		
		characters.add("Sniper");
		characters.add("Knight");
		characters.add("Sailor");
		characters.add("Hero");
		characters.add("Villager");
		
		inanimate.add("Inn");
		inanimate.add("Building");
		inanimate.add("Tree");
		inanimate.add("Mountain");
		inanimate.add("Rock");
		
		//Mapa Inicial
		Character o;
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(0,0,o);
		o=new Character();
		o.setName("Hero");
		o.setType("C");
		putObject(0,2,o);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(2,2,o);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(1,1,o);
		o=new Character();
		o.setName("Villager");
		o.setType("C");
		putObject(4,0,o);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(6,4,o);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(7,8,o);
		o=new Character();
		o.setName("Villager");
		o.setType("C");
		putObject(4,9,o);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(1,6,o);
		Inanimate i;
		i=new Inanimate();
		i.setName("Tree");
		i.setType("I");
		putObject(9,3,i);
		i=new Inanimate();
		i.setName("Mountain");
		i.setType("I");
		putObject(3,7,i);
		o=new Character();
		o.setName("Sniper");
		o.setType("C");
		putObject(4,5,o);
		i=new Inanimate();
		i.setName("Rock");
		i.setType("I");
		map.getCell(4, 1).setObject(i);
		o=new Character();
		o.setName("Knight");
		o.setType("C");
		putObject(8,3,o);
		i=new Inanimate();
		i.setName("Rock");
		i.setType("I");
                map.getCell(8, 9).setObject(i);
		o=new Character();
		o.setName("Villager");
		o.setType("C");
                putObject(2,9,o);
	}
	
	public void save(String name){
		
		XmlCreator xml=new XmlCreator();
		xml.setArrCell(new ArrayList<Cell>());
		
		xml.mapToArrayList(map);
		try{
		JAXBContext context = JAXBContext.newInstance(XmlCreator.class);
	    Marshaller m = context.createMarshaller();
	    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);   

	    // Write to File
	    File arch = new File(name);
	    m.marshal(xml,arch );
	    
		}catch (JAXBException e) {  
			   // some exception occurred  
			   e.printStackTrace();     
	   }
	}
	
	public void loadMap(String name){
		try {
			 
			File file = new File(name);
			if(file.exists()!=true){System.out.println("Error al cargar el mapa");}
			JAXBContext jaxbContext = JAXBContext.newInstance(XmlCreator.class);			
	 		Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();	
	 		
			XmlCreator xml= (XmlCreator) jaxbUnmarshaller.unmarshal(file);	
			map=xml.arrayListToMap();
			
		  } catch (JAXBException e) {
			e.printStackTrace();
		  }
	}
        public void setMap(Map m) {
		map = m;
	}
        
        
	public Map getMap() {
		return map;
	}

	public int read(Scanner entrada){
	
		int x;
		int y;
		String name;
		String type;
		int band;
				
		String inst=entrada.next();
		
		if (inst.equals("chT")){//chT posY posX TerrainType 
			x=entrada.nextInt();
			y=entrada.nextInt();
			type=entrada.next();			
			updateTerrain(x-1, y-1, Integer.parseInt(type));// (x, y, type);
			
			
		}
		else if(inst.equals("addO")){//addO posY posX Name
			x=entrada.nextInt();
			y=entrada.nextInt();
			name=entrada.next();
			if(searchIna(name)){
				Inanimate o = new Inanimate();
				o.setName(name);
				o.setType("I");				
				putObject(x-1, y-1, o);
			}else{
				System.out.println("Este objeto no existe");
			}
			
			
		}else if(inst.equals("addC")){//addC posY posX Name Band
			x=entrada.nextInt();
			y=entrada.nextInt();
			name=entrada.next();
			band=entrada.nextInt();
			if(searchChar(name)){
				Character o = new Character();
				o.setName(name);
				o.setType("C");
				o.setBand(band);
				putCharac (x-1, y-1, o);
			}else{
				System.out.println("Este personaje no existe");
			}	
			
			
		}else if(inst.equals("del")){
			x=entrada.nextInt();
			y=entrada.nextInt();
			delete(x-1, y-1);
		}else if(inst.equals("girarH")){
			x=entrada.nextInt();
			y=entrada.nextInt();
			girarH(x-1,y-1);
		}else if(inst.equals("girarA")){
			x=entrada.nextInt();
			y=entrada.nextInt();
			girarA(x-1,y-1);
		}
		else if(inst.equals("exit")){
			return 0;
		}
		else{
	
			return 0;
		}
		return 1;
	}
	
        public int updateTerrain(int x,int  y, int type){
            map.getCell(x,y).setType(type);
            return 1;
        }
        
        public int putObject (int x, int y, Object o){
            map.getCell(x,y).setObject(o);
            return 1;
	}
	
	public int putCharac (int x, int y, Object o){
            map.getCell(x, y).setObject(o);
            return 1;
	}
        public int delete (int x,int y){
            map.getCell(x, y).setObject(null);
            return 1;
        }
        
        public int girarA(int x,int y){
            Object o = map.getCell(x, y).getObject();
            if (o != null){
                o.girarA();
                return 1;
            }else
                System.out.println("No existe un objeto en esta posicion.");
            return 0;
        }
        public int girarH(int x,int y){
            Object o = map.getCell(x, y).getObject();
            if (o != null){
                o.girarH();
                return 1;
            }else
                System.out.println("No existe un objeto en esta posicion.");
            return 0;
        }
        
        
        public ArrayList<String> showChar(){
        	return characters;
	}
	
        public ArrayList<String> showIna(){
	return inanimate;
	}
        



        private boolean searchChar(String name){
		
		int size=characters.size();
		for(int i=0;i<size;i++){
			if(characters.get(i).equals(name)){
				return true;
			}
		}
		return false;
	}
	
	private boolean searchIna(String name){
		
		int size=inanimate.size();
		for(int i=0;i<size;i++){
			if(inanimate.get(i).equals(name)){
				return true;
			}
		}
		return false;
	}

	
	
}
